/*
MeeDocs - A Google Docs / Google Drive client for N9
Copyright 2012 Marcel D. Juhnke <marcel.juhnke@ovi.com>

This file is part of MeeDocs.

    MeeDocs is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 2 of the License, or
    (at your option) any later version.

    MeeDocs is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with MeeDocs.  If not, see <http://www.gnu.org/licenses/>.
*/

#ifndef MEEDOCS_H
#define MEEDOCS_H

#include <QDir>
#include <QFile>
#include <QFileInfo>
#include <QList>
#include <QNetworkAccessManager>
#include <QNetworkRequest>
#include <QNetworkReply>
#include <QStringList>
#include <QTextStream>
#include <QTimer>
#include <QUrl>
#include <QtGui/QApplication>

#include <TransferUI/Client>
#include <TransferUI/Transfer>

#include <maemo-meegotouch-interfaces/shareuiinterface.h>
#include <MDataUri>

#include <QtDebug>


 class MeeDocs: public QObject
 {
     Q_OBJECT
     QNetworkAccessManager manager;
     QNetworkReply *downReply; //NetworkReply Object for Downloads
     QNetworkReply *putReply; //NetworkReply Object for Downloads
     TransferUI::Client *transferClient; //Global client for TransferUI
     TransferUI::Transfer *downloadTransfer; //Tranfser pointer for download Transfers
     TransferUI::Transfer *uploadTransfer; //Tranfser pointer for download Transfers

 public:
     MeeDocs();

     //the following methods handle down- and uploads from and to GDrive
     Q_INVOKABLE void doDownload(const QUrl &url, const QString &accessToken, QString name, const QString &doctype);
     Q_INVOKABLE void doUpload(const QString &path, const QString &filename, const QString &accessToken);
     Q_INVOKABLE QByteArray uploadFileSize(const QString &path, const QString &filename);

     //the following methods handle GDrive collection/folder management
     Q_INVOKABLE void createCollection(const QString &collectionTitle, const QString &accessToken);
     Q_INVOKABLE void deleteCollection(const QString &collectionTitle, const QString &accessToken);
     Q_INVOKABLE void addToCollection(const QString &collectionId, const QString &resourceId, const QString &accessToken);
     Q_INVOKABLE void removeFromCollection(const QString &url, const QString &accessToken);
     Q_INVOKABLE void shareDoc(const QString &selflink, const QString &accessToken);
     Q_INVOKABLE void openShareUI(const QString &url);

     //the following methods handle GDrive file movement/deletion
     Q_INVOKABLE void trashFile(const QString &selfLink, const QString &accessToken);

     //the following methods handle local (on-device) file access for uploads
     Q_INVOKABLE QStringList listDirContent(const QString &path);
     Q_INVOKABLE bool isDir(const QString &path);


 signals:
     void downloadDone();
     void uploadDone();
     void uploadFailed();
     void nextBytes();

     void collectionCreated();
     void collectionFailed();

     void addedToCollection();
     void addedToCollectionFailed();


 public slots:
     void downloadFinished();
     void resumeUpload();

     void downloadStatus(const qint64 &bytesReceived, const qint64 &bytesTotal);
     void uploadStatus(const qint64 &bytesSent, const qint64 &bytesTotal);

     void cancelDownload();
     void cancelUpload();

     void isCollectionCreated();
     void isCollectionDeleted();
     void isAddedToCollection();
     void isRemovedFromCollection();

     void isFileTrashed();
     void isDocShared();

 private:
    QByteArray m_auth_header; //GData API authentication header

    QString m_filename; //filename for file downloads

    QByteArray m_file_size; //holds the file size of the file to be uploaded
    QByteArray m_content_type; //holds the content (MIME) type of the file to be uploaded
    qint64 m_content_range_begin; //holds the beginning of the byte range for the current upload chunk
    qint64 m_content_range_end; //holds the end of the byte range for the current upload chunk
    int m_file_parts; //holds the number of chunks of which the uploaded file consists
    int m_current_file_part; //holds the number of chunk which currently being uploaded
    QUrl m_next_location; //holds the URL to where the next upload chunk has to be PUT
    QByteArray m_succeeded_range; //holds the reply from GDrive which bytes have already been received successfully

    void nextUpload(const QUrl &url);
    bool saveToDisk(const QString &filename, QIODevice *data);
    QByteArray contentType(const QString &filename);



    QFile *m_upload_file; //pointer to the file which is to be uploaded

    qint64 m_bytes_received; //holds how much bytes have been downloaded for the Transfer UI
    qint64 m_bytes_total; //holds how much bytes the transfer is in total for the Transfer UI
    qint64 m_chunk_size; //holds the chunk size for the GDrive upload
    qint64 m_bytes_sent; //holds how much bytes have been uploaded for the Transfer UI

    //iterators and percentages for Transfer UI progress bars
    float m_download_progress_iterator;
    float m_download_progress;
    float m_upload_progress_iterator;
    float m_upload_progress;
    bool m_transfer_size_set; //holds if the transfer size for Transfer UI is alreadys set to avoid  continuous rewriting

 };

#endif // DOWNLOADMANAGER_H
